package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartSerice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车相关
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShopingCartController {
    @Autowired
    private ShoppingCartSerice shoppingCartSerice;

    /**
     * 添加到购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加商品")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("商品类型：{}",shoppingCartDTO);
        shoppingCartSerice.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getAll(){
         List<ShoppingCart> shoppingCartList = shoppingCartSerice.getAll();
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        shoppingCartSerice.clean();
        return Result.success();
    }

    /**
     * 删除购物车中的某个商品
     */
    @ApiOperation("删除购物车中的某个商品")
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartSerice.delete(shoppingCartDTO);
        return Result.success();
    }
}
